package cn.softeem.tushu.web;

import cn.softeem.tushu.Dao.OrderDao;
import cn.softeem.tushu.po.*;
import cn.softeem.tushu.service.CartitemService;
import cn.softeem.tushu.service.Impl.CartitemServiceImpl;
import cn.softeem.tushu.service.Impl.OrderServiceImpl;
import cn.softeem.tushu.service.OrderService;
import cn.softeem.tushu.utils.BaseServlet;
import cn.softeem.tushu.utils.PageBean;
import cn.softeem.tushu.utils.Tools;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@WebServlet(name = "OrderServlet", urlPatterns = "/OrderServlet")
public class OrderServlet extends BaseServlet {
    CartitemService cartitemService =new CartitemServiceImpl();
    OrderService orderService =new OrderServiceImpl();
    public String create(HttpServletRequest request, HttpServletResponse response){
        String cartItemIds = request.getParameter("cartItemIds");
        String address = request.getParameter("address");
        List<Cartitem> list = cartitemService.loadCartItems(cartItemIds);
        Order order =new Order();
        order.setOid(Tools.uuid());
        order.setOrdertime(String.format("%tF %<tT",new Date()));
        BigDecimal total = new BigDecimal("0");
        for (Cartitem cartitem : list) {
            total=total.add(new BigDecimal(Double.toString(cartitem.getSubtotal())));
        }
        order.setTotal(total.doubleValue());
        order.setStatus(1);
        order.setAddress(address);
        User user =(User) request.getSession().getAttribute("user");
        order.setUser(user);
        List<Orderitem> orderitemList =new ArrayList<>();
        for (Cartitem cartitem : list) {
            Orderitem orderitem =new Orderitem();
            orderitem.setOrderItemId(Tools.uuid());
            orderitem.setQuantity(cartitem.getQuantity());
            orderitem.setBook(cartitem.getBook());
            orderitem.setOrder(order);
            orderitemList.add(orderitem);
        }
        order.setOrderitems(orderitemList);
        orderService.save(order);
        cartitemService.delete(cartItemIds);
        request.setAttribute("order", order);
        return "f:/jsps/order/ordersucc.jsp";
    }

    public String myOrders(HttpServletRequest request, HttpServletResponse response){
        User user =(User) request.getSession().getAttribute("user");
        String pageNow =request.getParameter("pageNow");
        if (pageNow==null || pageNow ==""){
            pageNow="1";
        }
        PageBean<Order> order =orderService.getOrderList(user.getUid(),Integer.parseInt(pageNow));

        order.setUrl("OrderServlet?method=myOrders&uid="+user.getUid());
        request.setAttribute("bean",order);
        return "f:/jsps/order/list.jsp";
    }

    public String desc(HttpServletRequest request, HttpServletResponse response){
        String oid = request.getParameter("oid");
        String operation = request.getParameter("operation");
        Order order = orderService.desc(oid);
        request.setAttribute("order",order);
        request.setAttribute("operation",operation);
        return "f:/jsps/order/desc.jsp";
    }
    public String cancel(HttpServletRequest request, HttpServletResponse response){
        String oid = request.getParameter("oid");
        orderService.cancel(oid);
        request.setAttribute("msg","该订单已取消");
        request.setAttribute("code","success");
        return "f:/jsps/msg.jsp";
    }
    public String confirm(HttpServletRequest request, HttpServletResponse response){
        String oid = request.getParameter("oid");
        orderService.confirm(oid);
        request.setAttribute("msg","该订单已经确认收货");
        request.setAttribute("code","success");
        return "f:/jsps/msg.jsp";
    }
    public String pay(HttpServletRequest request, HttpServletResponse response){
        String oid = request.getParameter("oid");
        orderService.pay(oid);
        request.setAttribute("msg","支付成功");
        request.setAttribute("code","success");
        return "f:/jsps/msg.jsp";
    }
    public String prepareForPayment(HttpServletRequest req, HttpServletResponse resp) {
        String oid = req.getParameter("oid");
        req.setAttribute("order", orderService.desc(oid));
        return "f:/jsps/order/pay.jsp";
    }


}
